//
//  CXPickerView.m
//  各种自定义小功能
//
//  Created by 蔡翔 on 16/8/2.
//  Copyright © 2016年 蔡翔. All rights reserved.
//

#import "CXPickerView.h"
#import "CXPickerSheetView.h"

#define CX_screenWidth [UIScreen mainScreen].bounds.size.width
#define CX_screenHeight [UIScreen mainScreen].bounds.size.height
#define CXPickerSheetViewHeight CX_screenHeight * 0.4

@interface CXPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSArray *_data_Arr;
    NSString *_selected_String;
    NSInteger _index;
}
/** 遮盖 */
@property (nonatomic, strong) UIButton *coverView;

@end

@implementation CXPickerView

/** 动画时间 */
static CGFloat const CX_animateWithDuration = 0.25;

- (instancetype)initWithDataArray:(NSArray <NSString *>*)data;
{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, CX_screenWidth, CX_screenHeight);
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        // 遮盖
        self.coverView = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.0;
        [_coverView addTarget:self action:@selector(dismissPickerView) forControlEvents:UIControlEventTouchUpInside];
        _coverView.frame = CGRectMake(0, 0, CX_screenWidth, CX_screenHeight);
        [self addSubview:self.coverView];
        // 获取数据
        _data_Arr = data;
    }
    return self;
}

- (BOOL)anySubViewScrolling:(UIView *)view{
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)view;
        if (scrollView.dragging || scrollView.decelerating) {
            return YES;
        }
    }
    for (UIView *theSubView in view.subviews) {
        if ([self anySubViewScrolling:theSubView]) {
            return YES;
        }
    }
    return NO;
}
#pragma mark - - - 按钮的点击事件
- (void)sureBtnClick {
    if (![self anySubViewScrolling:self.selectPickerSheetView]) {
        if (![self.selectPickerSheetView.pickerView selectedRowInComponent:0]) {
            if (self.selectMessage && _data_Arr.count > 0) {
                self.selectMessage(_data_Arr[0],0);
            }
        }else{
            if (self.selectMessage && _selected_String) {
                self.selectMessage(_selected_String,_index);
            }
        }
        [self dismissPickerView];
    }
}

// 消失
- (void)dismissPickerView {
    [UIView animateWithDuration:CX_animateWithDuration animations:^{
        self.selectPickerSheetView.transform = CGAffineTransformMakeTranslation(0, CXPickerSheetViewHeight);
        self.coverView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.selectPickerSheetView removeFromSuperview];
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

// 出现
- (void)show {
    [self setupCXPickerSheetView];
    [UIView animateWithDuration:CX_animateWithDuration animations:^{
        self.selectPickerSheetView.transform = CGAffineTransformMakeTranslation(0, - CXPickerSheetViewHeight);
        self.coverView.alpha = 0.3;
    }];
}

- (void)setupCXPickerSheetView {
    self.selectPickerSheetView = [[[NSBundle mainBundle] loadNibNamed:@"CXPickerSheetView" owner:nil options:nil] firstObject];
    _selectPickerSheetView.frame = CGRectMake(0, CX_screenHeight, CX_screenWidth, CXPickerSheetViewHeight);
    _selectPickerSheetView.pickerView.delegate = self;
    _selectPickerSheetView.pickerView.dataSource = self;
    [_selectPickerSheetView addTargetCancelBtn:self action:@selector(dismissPickerView)];
    [_selectPickerSheetView addTargetSureBtn:self action:@selector(sureBtnClick)];
    [self addSubview:_selectPickerSheetView];
}

- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values_Arr = [NSMutableArray array];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values_Arr addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values_Arr;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [view.layer addAnimation:animation forKey:nil];
}

#pragma mark - - - UIPickerViewDataSource - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _data_Arr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return _data_Arr[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return CX_screenWidth;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (_data_Arr.count > 0) {
        _selected_String = _data_Arr[row];
        _index = row;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label   = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CX_screenWidth, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    if (_data_Arr.count > 0) {
        label.text = [_data_Arr objectAtIndex:row];
    }
    label.font = [UIFont systemFontOfSize:16];
    label.backgroundColor = [UIColor clearColor];
    return label;
}
@end


